package regpang.myapplication22.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import regpang.myapplication22.R;

public class DialogForFragment extends DialogFragment {
    private String adress;
    private String deviceId;
    private String deviceType;

    public static DialogForFragment newInstance() {
        Bundle args = new Bundle();
        args.putString("adress", "合肥市尚泽大都会中心广场三楼营业厅(北门对面)");
        args.putString("deviceId", "EP-0001");
        args.putString("deviceType", "RAC_87A");
        DialogForFragment fragment = new DialogForFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //在DialogFragment的onCreateView里面设置，可以将对话框内部的背景设为透明
//        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        View view = inflater.inflate(R.layout.dialog_dialogfragment, null);
        view.findViewById(R.id.fl_navi).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        TextView tvDeviceId = view.findViewById(R.id.tv_device_id);
        TextView tvDeviceType = view.findViewById(R.id.tv_device_type);
        TextView tvAdress = view.findViewById(R.id.tv_address);
        tvDeviceId.setText("型号：" + deviceId);
        tvDeviceType.setText("编号：" + deviceType);
        tvAdress.setText(adress);
        return view;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        // 不带style的构建的dialog宽度无法铺满屏幕
        //     Dialog dialog = new Dialog(getActivity());
        Dialog dialog = new Dialog(getActivity(), R.style.CustomDatePickerDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_dialogfragment);
        dialog.setCanceledOnTouchOutside(true); // 设置弹出框布局参数，宽度铺满全屏，底部。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(wlp);
        return dialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        //在DialogFragment的onstart里面设置，可以将对话框外部的背景设为透明
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.dimAmount = 0.0f;
        window.setAttributes(windowParams);

        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle arguments = getArguments();
        adress = arguments.getString("adress");
        deviceId = arguments.getString("deviceId");
        deviceType = arguments.getString("deviceType");
    }

}
